<?php

/**
 * This is the model class for table "iteam_order".
 *
 * The followings are the available columns in table 'iteam_order':
 * @property string $oid
 * @property integer $uid
 * @property integer $pay_type
 * @property double $total
 * @property integer $did
 * @property integer $coin
 * @property integer $freight
 * @property double $total_final
 * @property string $consignee
 * @property string $phone
 * @property string $email
 * @property string $pre
 * @property string $address
 * @property integer $hongyun_return
 * @property integer $coin_return
 * @property integer $receipt_type
 * @property string $receipt_field1
 * @property string $receipt_field2
 * @property integer $status
 * @property string $dateline
 * @property string $updated
 */
class Order extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Order the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'iteam_order';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid, pay_type, did, coin, freight, hongyun_return, coin_return, receipt_type, status', 'numerical', 'integerOnly' => true),
            array('total, total_final', 'numerical'),
            array('consignee, phone, email, pre', 'length', 'max' => 50),
            array('address', 'length', 'max' => 200),
            array('receipt_field1, receipt_field2', 'length', 'max' => 100),
            array('dateline, updated', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('oid, uid, pay_type, total, did, coin, freight, total_final, consignee, phone, email, pre, address, hongyun_return, coin_return, receipt_type, receipt_field1, receipt_field2, status, dateline, updated', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'items' => array(self::HAS_MANY, 'OrderItem', 'oid'),
            'user' => array(self::BELONGS_TO, 'User', 'uid'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'oid' => '訂單編號',
            'uid' => '用戶ID',
            'pay_type' => '付款方式',
            'total' => '商品總價',
            'did' => 'Did',
            'coin' => '購物金',
            'freight' => '運費',
            'total_final' => '訂單金額',
            'consignee' => '收件人',
            'phone' => '電話',
            'email' => '郵箱',
            'pre' => '稱謂',
            'address' => '地址',
            'hongyun_return' => '返點',
            'coin_return' => '返購物金',
            'receipt_type' => '發票類型',
            'receipt_field1' => 'Receipt Field1',
            'receipt_field2' => 'Receipt Field2',
            'status' => '處理狀態',
            'dateline' => '創建時間',
            'updated' => '更新時間',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('oid', $this->oid, true);
        $criteria->compare('uid', $this->uid);
        $criteria->compare('pay_type', $this->pay_type);
        $criteria->compare('total', $this->total);
        $criteria->compare('did', $this->did);
        $criteria->compare('coin', $this->coin);
        $criteria->compare('freight', $this->freight);
        $criteria->compare('total_final', $this->total_final);
        $criteria->compare('consignee', $this->consignee, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('pre', $this->pre, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('hongyun_return', $this->hongyun_return);
        $criteria->compare('coin_return', $this->coin_return);
        $criteria->compare('receipt_type', $this->receipt_type);
        $criteria->compare('receipt_field1', $this->receipt_field1, true);
        $criteria->compare('receipt_field2', $this->receipt_field2, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline, true);
        $criteria->compare('updated', $this->updated, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = $this->updated = date('Y-m-d H:i:s');
            }
            $this->updated = date('Y-m-d H:i:s');
        }
        return true;
    }

}